﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using Models;
using System.Web.Script.Serialization;


namespace 公司CRM客户关系系统.Sale
{
    /// <summary>
    /// SaleOpen 的摘要说明
    /// </summary>
    public class SaleOpen : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //得到action
            string action = context.Request["action"];
            if (action == "selectall")
            { //查询全部

                List<Plans> list = BaseDAL.SelectAll<Plans>();
                //转为json字符串
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(list);
                //输出
                context.Response.Write(json);
            }
            else if (action == "find")
            {
                //得到ID
                int id = int.Parse(context.Request["id"]);
                //查询
                List<Plans> p = BaseDAL.Select<Plans>("select * from Plans where ChanID="+id,null);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(p);
                //输出结果
                context.Response.Write(json);
            }
            else if (action == "find2")
            {
                //得到ID
                int id = int.Parse(context.Request.QueryString["id"]);
                //查询
                Chances p = BaseDAL.Find<Chances>(id);
                //转为json
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(p);
                //输出结果
                context.Response.Write(json);
            }
            else if (action == "update")
            {
                //===如果没有修改生日和身高，则代码就不能简单的写，应该复杂的这样写：
                //得到提交的ID
                int id = int.Parse(context.Request.Form["ChanID"]);
                string t1 = context.Request.Form["PlanContent"];
                Plans p = new Plans();
                p.ChanID = id;
                p.PlanDate = Convert.ToDateTime(System.DateTime.Now.ToString("D"));
                p.PlanContent = t1;
                int row = BaseDAL.Insert(p);
                //返回结果
                context.Response.Write(row);

            }
            else if (action == "update1")
            {
                //得到提交的ID
                int id1 = int.Parse(context.Request.Form["PlanID"]);
                int id = int.Parse(context.Request.Form["ChanID"]);
                string t1 = context.Request.Form["PlanResult"];
                Plans p = BaseDAL.Find<Plans>(id1);
                p.PlanResult = t1;
                p.PlanResultDate = Convert.ToDateTime(System.DateTime.Now.ToString("D"));
                //修改
                int row = BaseDAL.Update(p);
                //返回结果
                context.Response.Write(row);

            }
            else if (action == "update2")
            {
                //得到提交的ID
                int id = int.Parse(context.Request.Form["ChanID"]);
                Chances ch = BaseDAL.Find<Chances>(id);
                ch.ChanState = 3;
                
                //往客户信息添加一条数据
                Customers c = new Customers();
                string newCusID = "DY";
                string NewDate = DateTime.Now.ToString("yyyyMMdd");
                string sql = "select top 1 * from Customers order by CusID desc";
                ////最大的订单号
                string maxCusID = BaseDAL.ExecuteScalar(sql, null).ToString();
                //截取日期部分
                if (maxCusID != "" && maxCusID.Substring(2, 8) == NewDate)//如果当天存在了订单
                {
                    //就在当天订单的后面+1
                    //1000?
                    int ls = Convert.ToInt32(maxCusID.Substring(maxCusID.Length - 4)) + 1+1000; //获取最后4位流水码
                    //流水码
                    string lsm = ls.ToString().Substring(1);
                    //新订单
                    newCusID = newCusID + NewDate + lsm;
                    c.CusID = newCusID;
                }
                else//如果当天不存在订单
                {
                    //新订单
                    newCusID = newCusID + NewDate + "001";
                    c.CusID = newCusID;
                }
                c.UserID = Convert.ToInt32((context.Session["user"] as V_Users).UserID);
                c.CusName = ch.ChanName;
                c.CusDate = DateTime.Now;
                c.CusState = Convert.ToInt32(1);
                int r = BaseDAL.Insert(c);

                //修改
                int row = BaseDAL.Update(ch);
                //返回结果
                context.Response.Write(row);

            }
            else if (action == "update3")
            {
                //得到提交的ID
                int id = int.Parse(context.Request.Form["ChanID"]);
                string t1 = context.Request.Form["ChanError"];
                Chances p = BaseDAL.Find<Chances>(id);
                p.ChanState = 4;
                p.ChanError = t1;
                //修改
                int row = BaseDAL.Update(p);
                //返回结果
                context.Response.Write(row);

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}